package com.itheima.health.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleMapper;
import com.itheima.health.dao.RoleMenuMapper;
import com.itheima.health.dao.RolePermissionMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.RoleMenu;
import com.itheima.health.pojo.RolePermission;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@DubboService
@Transactional
public class RoleServiceImpl implements RoleService {
	@Autowired
	RoleMapper roleMapper;
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	@Autowired
	RolePermissionMapper rolePermissionMapper;
	@Override
	public List<Role> findAllRoles() {
		List<Role> roleList = roleMapper.selectAll();
		return roleList;
	}

	@Override
	@Transactional
	public void delete(Integer id) {
		RolePermission rolePermission = new RolePermission();
		rolePermission.setRoleId(id);
		rolePermissionMapper.delete(rolePermission);
		roleMapper.deleteByPrimaryKey(id);
	}

	/**
	 * @Author: jay
	 * @Date: 9:13 2021/3/12
	 * @Params [role, menuIds, permissionIds]
	 * @ReturnType: void
	 * @Description: 角色管理-新增
	 */
	@Override
	@Transactional
	public void add(Role role, Integer[] menuIds, Integer[] permissionIds) {
		roleMapper.insertSelective(role);
		for (Integer menuId : menuIds) {
			RoleMenu roleMenu = new RoleMenu();
			roleMenu.setRoleId(role.getId());
			roleMenu.setMenuId(menuId);
			roleMenuMapper.insertSelective(roleMenu);
		}
		for (Integer permissionId : permissionIds) {
			RolePermission rolePermission = new RolePermission();
			rolePermission.setRoleId(role.getId());
			rolePermission.setPermissionId(permissionId);
			rolePermissionMapper.insertSelective(rolePermission);
		}
	}

	/**
	 * @Author: jay
	 * @Date: 9:36 2021/3/12
	 * @Params [id]
	 * @ReturnType: com.itheima.health.pojo.Role
	 * @Description: 角色管理-根据id查询
	 */
	@Override
	public Role findByid(Integer id) {
		return roleMapper.selectByPrimaryKey(id);
	}


	/**
	 * @Author: jay
	 * @Date: 9:50 2021/3/12
	 * @Params [id]
	 * @ReturnType: java.util.List<java.lang.Integer>
	 * @Description: 角色-根据id查询菜单id
	 */
	@Override
	public List<Integer> findMenuIdsByRoleId(Integer id) {
		RoleMenu roleMenu = new RoleMenu();
		roleMenu.setRoleId(id);
		List<RoleMenu> roleMenuList = roleMenuMapper.select(roleMenu);
		List<Integer> menuIds = new ArrayList<>();
		for (RoleMenu roleMenu1 : roleMenuList) {
			menuIds.add(roleMenu1.getMenuId());
		}
		return menuIds;
	}

	/**
	 * @Author: jay
	 * @Date: 9:50 2021/3/12
	 * @Params [id]
	 * @ReturnType: java.util.List<java.lang.Integer>
	 * @Description: 角色-根据id查询权限id
	 */
	@Override
	public List<Integer> findPermissionIdsByRoleId(Integer id) {
		RolePermission rolePermission = new RolePermission();
		rolePermission.setRoleId(id);
		List<RolePermission> rolePermissions = rolePermissionMapper.select(rolePermission);
		List<Integer> permissionIds = new ArrayList<>();
		for (RolePermission rolePermission1 : rolePermissions) {
			permissionIds.add(rolePermission1.getPermissionId());
		}
		return permissionIds;
	}

	/**
	 * @Author: jay
	 * @Date: 10:07 2021/3/12
	 * @Params [role, menuIds, permissionIds]
	 * @ReturnType: void
	 * @Description: 角色管理-修改
	 */
	@Override
	@Transactional
	public void edit(Role role, Integer[] menuIds, Integer[] permissionIds) {
		roleMapper.updateByPrimaryKeySelective(role);
		RoleMenu roleMenu = new RoleMenu();
		roleMenu.setRoleId(role.getId());
		roleMenuMapper.delete(roleMenu);
		for (Integer menuId : menuIds) {
			roleMenu.setMenuId(menuId);
			roleMenuMapper.insertSelective(roleMenu);
		}
		RolePermission rolePermission = new RolePermission();
		rolePermission.setRoleId(role.getId());
		rolePermissionMapper.delete(rolePermission);
		for (Integer permissionId : permissionIds) {
			rolePermission.setPermissionId(permissionId);
			rolePermissionMapper.insertSelective(rolePermission);
		}
	}

	@Override
	public PageResult findPage(QueryPageBean queryPageBean) {
		PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
		String queryString = queryPageBean.getQueryString();
		Example example = new Example(Role.class);
		Example.Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(queryString)) {
			criteria.orLike("name", "%" + queryString + "%");
			criteria.orLike("keyword", "%" + queryString + "%");
		}
		Page<Role> page = (Page<Role>) roleMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}
}
